package com.company.woye.hotmove.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.R;
import com.company.woye.hotmove.bean.HotMoveSearch;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HotMoveSearchAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<HotMoveSearch> dbhListBeans;

    public HotMoveSearchAdapter(Context context, List<HotMoveSearch> dbhListBeans) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.dbhListBeans = dbhListBeans;
    }

    @Override
    public int getCount() {
        return dbhListBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return dbhListBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hot_search_detail, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        if (dbhListBeans.size() > 0) {
            HotMoveSearch hotMoveSearch = dbhListBeans.get(position);
            viewHodler.cb.setTag(position);
            viewHodler.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        dbhListBeans.get((int) viewHodler.cb.getTag()).setCheck(true);
                    } else {
                        dbhListBeans.get((int) viewHodler.cb.getTag()).setCheck(false);
                    }
                }
            });
            viewHodler.cb.setChecked(dbhListBeans.get(position).isCheck());
            viewHodler.tvColor.setText(hotMoveSearch.getColor());
            viewHodler.tvOrderNo.setText(hotMoveSearch.getOrderNo());
            viewHodler.tvSize.setText(hotMoveSearch.getSize());
            viewHodler.tvStatus.setText(hotMoveSearch.getCurState());
            viewHodler.tvAllbox.setText(hotMoveSearch.getBoxNum());
            viewHodler.tvYYNum.setText(hotMoveSearch.getYYNum());
            viewHodler.tvDBH.setText(hotMoveSearch.getDBH());
            if (!TextUtils.isEmpty(hotMoveSearch.getBackColor())) {
                viewHodler.tvStatus.setBackgroundColor(GetColorValue(hotMoveSearch.getBackColor()));
            }
            String boxNo=hotMoveSearch.getBoxNo();
            if (!TextUtils.isEmpty(boxNo)){
                viewHodler.tvAllbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(mContext,boxNo,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
        return convertView;
    }


    //从字符串转颜色
    public int GetColorValue(String paramColor) {
        int PenColor = Color.WHITE;
        if (paramColor.length() > 0) PenColor = Color.parseColor(paramColor);
        return PenColor;
    }



    class ViewHolder {
        @BindView(R.id.tv_orderNo)
        TextView tvOrderNo;
        @BindView(R.id.tv_DBH)
        TextView tvDBH;
        @BindView(R.id.tv_color)
        TextView tvColor;
        @BindView(R.id.tv_size)
        TextView tvSize;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_allbox)
        TextView tvAllbox;
        @BindView(R.id.tv_YYNum)
        TextView tvYYNum;
        @BindView(R.id.cb)
        CheckBox cb;
        @BindView(R.id.ll)
        LinearLayout ll;
        @BindView(R.id.lltop)
        LinearLayout lltop;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
